Skip to content

增加了无需登陆情况下,根据用户名获取用户关注情况的api#32

Open
tomguluson92 wants to merge 7 commits intolzjun567:masterfrom
tomguluson92:tomguluson92-patch-1
Open

增加了无需登陆情况下,根据用户名获取用户关注情况的api#32
tomguluson92 wants to merge 7 commits intolzjun567:masterfrom
tomguluson92:tomguluson92-patch-1

Conversation

@tomguluson92
Copy link
Copy Markdown

更改了4个文件:
1.README
2.url.py
3.zhihu/models/init.py
4.zhihu/models/common.py

Comment thread README.md
**关注用户和被关注用户**

```
>>> zhihu.follows("高日日")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

follow vs follows,小伙子改个名吧。。。

Comment thread zhihu/models/__init__.py
url = 'https://www.zhihu.com/search?type=people&q={}'.format(Name)
data = requests.get(url, headers=Headers)
soup = BeautifulSoup(data.text, 'lxml')
yonghu = soup.select('a[class="name-link author-link"]')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yonghu 是什么鬼。。。user 是哪里不好?

Comment thread zhihu/models/__init__.py
yonghu = soup.select('a[class="name-link author-link"]')
pattern = '<a.*?href="([^"]*)".*?>(?:[\S\s]*?)</a>'
token = None
for i in yonghu:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

两个点。

  1. 既然下面用 for 来迭代了,这里显然应该是用复数:usersuser_list 之类的东西。
  2. for 循环中,用 i 通常都是 index 的缩写,这里显然是对应的 user 对象。所以,user 就好。for user in users: pass

Comment thread zhihu/models/__init__.py
i = str(i)
mat = re.findall(pattern, i)
token = mat
if token[0]:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的逻辑是,找到第一个 match(对,mat 也没几个人知道是啥,不要随便缩写。。。)然后直接 return match[0]。所以,其实 token 这个变量完全没有存在的意义。下面这样写就够了。

for user in users:
    match = re.findall(pattern, str(user))
    if match[0]:
        return match[0]
return None

Comment thread zhihu/models/common.py

>>> follows(user_name = "高日日")
"""
if not user_name:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既然要求一定要填 user_name ,为什么要填默认值。。。

Comment thread zhihu/models/common.py

user_slug = self._get_token(user_name)

response = requests.get(URL.user_followed_number(user_slug), headers={'User-Agent':
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

headers 的值应该有固定的?如果没有的话,找个地方放一下,然后在这里(以及其他地方)引用

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

恩,因为作者之前的失效了,我自己测试随便写了一个。

Comment thread zhihu/models/common.py

dicts = dict()
counts = 0
for i in init_data_renshu:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里直接写两遍 i.get_next() 就可以了吧。。。没必要用 for 循环,看着很别扭。还强行设置了一个 counts...
顺便,小伙子你大概不知道 enumerate 这个东西?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry,比较菜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants